<template>
  <div class="p-20">
    <el-alert type="success" effect="dark" title="ElementPlus框架没有日程组件，这里使用calendar组件代替"></el-alert>
    <div class="tile-card p-20 mt-20">
      <el-calendar ref="scheduleRef">
        <template #header="{ date }">
          <div style="font-size: 14px; display: flex; align-items: center">
            <div clas="flex align-center"><Calendar /> <span>我的日程</span></div>
            <el-button style="margin-left: 15px" round @click="dialogVisible = true">添加日程</el-button>
          </div>
          <el-button-group>
            <el-button @click="selectDate('prev-month')">上个月</el-button>
            <el-button @click="selectDate('today')">今天</el-button>
            <el-button @click="selectDate('next-month')">下个月</el-button>
          </el-button-group>
        </template>
        <template #date-cell="{ data }">
          <div class="flex flex-column" style="height: 100%">
            <p>
              {{ data.day.split('-').slice(1).join('-') }}
            </p>
            <ul class="schedules flex-1">
              <li v-for="item in schedules.filter((v) => v.date == data.day)">{{ item.title }}</li>
            </ul>
          </div>
        </template>
      </el-calendar>
    </div>
    <el-dialog v-model="dialogVisible" title="添加日程" width="50%">
      <div class="dialog-content pt-20">
        <el-form :model="scheduleForm" ref="formRef" :rules="rules" label-width="100px">
          <el-form-item label="日程标题" prop="title">
            <el-input v-model="scheduleForm.title" placeholder="请输入日程标题" clearable />
          </el-form-item>
          <el-form-item label="日程日期" prop="date">
            <el-date-picker
              v-model="scheduleForm.date"
              placeholder="请选择日程日期"
              :disabled-date="disabledDate"
              clearable />
          </el-form-item>
          <el-form-item label="日程内容" prop="content">
            <el-input
              type="textarea"
              v-model="scheduleForm.content"
              placeholder="请输入日程内容"
              :rows="5"
              clearable></el-input>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="confirm">确认</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { addSchedules, getSchedules } from '@/mocks/personal';
import { onMounted, reactive, ref } from 'vue';
import { ElMessage, FormRules } from 'element-plus';
import { Calendar } from '@icon-park/vue-next';

const currentDate = ref(new Date());
const loading = ref(false);
const schedules = ref([]);
const scheduleRef = ref();
const dialogVisible = ref(false);
const formRef = ref();
const scheduleForm = ref({
  title: '',
  content: '',
  date: ''
});
const disabledDate = (time: Date) => {
  const now = new Date(); // 获取当前时间
  now.setHours(0, 0, 0, 0); // 设置时间为当天的0点整
  return time.getTime() < now.getTime();
};

const rules = reactive<FormRules<any>>({
  title: { required: true, message: '请输入日程标题', trigger: 'blur' },
  date: { required: true, type: 'date', message: '请选择日程日期', trigger: 'change' }
});

function selectDate(value) {
  scheduleRef.value.selectDate(value);
}

function _getSchedules() {
  loading.value = true;
  getSchedules().then((res) => {
    schedules.value = res.data;
    loading.value = false;
    console.log(schedules);
  });
}

function _resetForm() {
  scheduleForm.value = {
    title: '',
    content: '',
    date: ''
  };
}

function confirm() {
  formRef.value.validate((valid) => {
    if (!valid) return;
    addSchedules(scheduleForm.value).then((_) => {
      _resetForm();
      dialogVisible.value = false;
      _getSchedules();
      ElMessage.success('创建日程成');
    });
  });
}
function cancel() {
  dialogVisible.value = false;
  _resetForm();
}

onMounted(() => {
  _getSchedules();
});
</script>
<style lang="scss" scoped>
.schedules {
  height: 100%;
  overflow-y: auto;
  li {
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
    font-size: 12px;
    line-height: 1;
    margin-top: 4px;

    &::before {
      display: inline-block;
      content: '';
      width: 4px;
      height: 4px;
      vertical-align: middle;
      background-color: #f00;
      border-radius: 50%;
      margin-right: 2px;
    }
  }
}
</style>
